;
function tJGoods() {
    var url = $('#goods-tj').data('url');
    var goodsLink = $('#goods-tj').data('link');
    var goodsTpl = ' <a target="_blank" class="tj-goods-item" href="{%url%}"><dl class="am-text-center am-margin-bottom-sm">\n' +
        '                                    <dt class="am-pdding-top am-padding-bottom-lg item-goods-img"><img src="{%goods_img%}"></dt>\n' +
        '                                    <dd class="am-text-default">\n' +
        '                                        <div class="am-padding-bottom-sm item-goods-title" style="color: #845f3f;">{%goods_title%}</div>\n' +
        '                                        <div class="am-bg1 am-padding-xs">\n' +
        '                                            <p class="item-goods-name">{%goods_name%}</p>\n' +
        '                                            <p class="am-text-danger">￥{%goods_price%}</p>\n' +
        '                                        </div>\n' +
        '                                    </dd></dl></a>'
    
    $.get(url,function (response) {
        if(response.status){
            var goodsItem = '';
            $.each(response.data,function (i,item) {
                goodsItem += goodsTpl.replace('{%goods_name%}',item.goods_name)
                    .replace('{%url%}',goodsLink.replace('-100',item.goods_id))
                    .replace('{%goods_title%}',item.title)
                    .replace('{%goods_price%}',item.price)
                    .replace('{%goods_img%}',str2arr(item.goods_images)[0])
            });
            $('#goods-tj').append(goodsItem);
        }
    })
}

function eventBuyNum() {
    var elem  =$('.buy-num input[name="number"]');
    var number = parseInt(elem.val());
    $('.num-dec').on('click',function () {
        number = parseInt(elem.val());
        if(number<=1){
            return false;
        }
        number = number-1;
        elem.val(number);
    });
    $('.num-add').on('click',function () {
        number = parseInt(elem.val());
        number = number+1;
        elem.val(number);
    });
}

function eventSpec() {
    $('.goods-specs .spec-value-box').each(function () {
        $(this).off('click').on('click',function () {
            $(this).addClass('active').children('em').addClass('selected')
            $(this).siblings('.spec-value-box').removeClass('active').children('em').removeClass('selected');
            getSpecs();
        });
    });
}

function getSpecs() {
    var specs = new Array();
    $('.goods-specs .spec-value-box.active').each(function () {
        specs.push($(this).children('i').text());
    });
    $('input[name="goods_specs"]').val(specs.join(','));
}

function detailImgLook() {
    var $miaobian=$('.img-item-list>div');
    var $huantu=$('.img-box>img:first');
    var $huantu1=$('.img-box .big-img');
    var $miaobian1=$('.color-group>li');
    $miaobian.mousemove(function(){miaobian(this);});
    $miaobian1.click(function(){miaobian1(this);});
    function miaobian(thisMb){
        for(var i=0; i<$miaobian.length; i++){
            //debugger;
            $miaobian[i].style.borderColor = '#dedede';
        }
        thisMb.style.borderColor = '#cd2426';

        $huantu[0].src = thisMb.children[0].src;
        $huantu1[0].src = thisMb.children[0].src;
    }
    function miaobian1(thisMb1){
        for(var i=0; i<$miaobian1.length; i++){
            $miaobian1[i].style.borderColor = '#dedede';
        }
        $miaobian.css('border-color','#dedede');
        thisMb1.style.borderColor = '#cd2426';
        $huantu[0].src = thisMb1.children[0].src;
    }
}

function buyNow(obj) {
    var action = $(obj).data('action');
    var number = parseInt($('input[name="number"]').val());
    var goodsSpec =  $('input[name="goods_specs"]').val();
    var isgoodsSpec =  $('input[name="is_goods_specs"]').val();
    if(number<1){
        layer.alert('请选择购买数量');
        return false;
    }
    if(parseInt(isgoodsSpec) > 0){
        if(goodsSpec == '' || goodsSpec == undefined){
            layer.alert('请选择购买规格');
            return false;
        }
    }

    $('#submit-goods').attr('action',action);
    $('#submit-goods').submit();
    return  true;
}

function addCart(obj) {
    var action = $(obj).data('action');
    var nextUrl = $(obj).data('next');
    var number = parseInt($('input[name="number"]').val());
    var goodsSpec =  $('input[name="goods_specs"]').val();
    var isgoodsSpec =  $('input[name="is_goods_specs"]').val();
    if(number<1){
        layer.alert('请选择购买数量');
        return false;
    }

    if(parseInt(isgoodsSpec) > 0){
        if(goodsSpec == '' || goodsSpec == undefined){
            layer.alert('请选择购买规格');
            return false;
        }
    }


   objectFlyIn($(obj),$('#right_cart'),$('#main-img').attr('src'));
   $.post(action,$('#submit-goods').serialize(),function (response) {
       if(response.status == 1){
           changeCartShowNum();
           layer.open({
               content: '商品添加成功!'
               ,btn: ['继续购物', '去购物车结算']
               ,yes: function(index, layero){
                  layer.close(index);
                  return false;
               }
               ,btn2: function(index, layero){

                   window.location.href = nextUrl;
                   return false ;
               }
           });
           return  true;
       }else{
           layer.alert('添加失败');
           return false;
       }
   });

}

function changeCartShowNum() {
    var hasGoods = $('#right_cart .J-count').text();
    $('#right_cart .J-count').text(parseInt(hasGoods)+1);
    $('.mycar .num').text(parseInt(hasGoods)+1);
}

// $("#divSource input[value='remove']").bind("click",function(){
//     var _sourceImg=$(this).prev().prev();
//     var _back=function(){};
//     var _target=$("#divTarget");
//     objectFlyOut(_sourceImg,_target,_back);
// });
/**
 * 对象飞入
 *
 */
function objectFlyIn(startObj,endObj, imgSrc) {

    var endOffset =endObj.offset();
    var startOffset = startObj.offset();
    $(window).resize(function () {
        $(window).scrollTop(0);
         endOffset =endObj.offset();
         startOffset = startObj.offset();
    });

    var scrollTop = 0;
    $(window).scroll(function() {
        scrollTop=$(window).scrollTop();
    });



    var flyer = $('<img style="display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 100000;" src="' + imgSrc + '">');

    flyer.fly({
        start: {
            left: startOffset.left+10, //开始位置（必填）
            top: startOffset.top-scrollTop //开始位置（必填）
        },
        end: {
            left: endOffset.left, //结束位置（必填）
            top: endOffset.top, //结束位置（必填）
            width: 50, //结束时宽度
            height: 50 //结束时高度
        },
        speed: 1,
        vertex_Rtop:-100,
        onEnd: function () { //结束回调
            this.destroy(); //移除dom
        }
    });

}

/**
 * 对象飞出
 *
 */
function objectFlyOut(_sourceImg,_target, _back) {
    var addOffset = _target.offset();

    var img = _sourceImg;
    var flyer = $('<img style="display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 100000;" src="' + img.attr('src') + '">');

    flyer.fly({
        start: {
            left: addOffset.left, //开始位置（必填）
            top: addOffset.top //开始位置（必填）
        },
        end: {
            left: addOffset.left-20, //结束位置（必填）
            top: addOffset.top-20, //结束位置（必填）
            width: 5, //结束时宽度
            height: 5 //结束时高度
        },
        onEnd: function () { //结束回调
            this.destroy(); //移除dom
            _back();
        }
    });
}